dojo.provide("dojot.album.AlbumOperate");
dojo.require("dijit.Dialog");
dojo.require("dijit.form.Button");
dojo.require("dojo.i18n");
dojo.requireLocalization("dojot", "util");
dojo.requireLocalization("dojot", "common");

dojo.declare("dojot.album.AlbumPerate",null,{
	fadeOut:null,
	fadeIn:null,
	_messages:null,
	picId:-1,
	constructor: function(obj){
		dojo.minxin(this,obj);
		var message1 = dojo.i18n.getLocalization("dojot", "common",dojo.locale);
		var message2 = dojo.i18n.getLocalization("dojot", "album",dojo.locale);
		dojo.mixin(this._messages,message1,message2);
		this.fadeOut = dojo.fadeOut({node: "errors",duration: 1000});
		this.fadeIn = dojo.fadeIn({node: "errors",duration: 1000});
	},
	clueInfo: function(clueId,clueInfo){
		dojo.byId(clueId).innerHTML = clueInfo;
		dojo.style(clueId,'display','block');
		currentAnimation = dojo.fx.chain([fadeIn,fadeOut]);
		currentAnimation.play();
	},
	getIds: function(){
		
		if(this.picId){
			return this.picId;
		}
		
		var temp = [];
		dojo.query("input:checked").forEach(function(data){
		    temp.push(data.value);
		});
		if(temp.length == 0){
	 		clueInfo('errors',this._messages.album2AlbumDialogjsSelectedOnePic);
		}
		return temp.join(",");
	},
	postService:function(server,callback){
		var picIds = this.getIds();
		dojo.xhrPost({
			url:server, 
			load:function(responseDate){
			if(responseDate.success){
				if(callback){
					callback();	
				}				
				window.location.reload();					
			}else{
				clueInfo('errors','error');
			}
			} ,
			handleAs: "json"
		});
	}
});

dojo.declare("dojot.album.albumTransferOperate",dojot.album.AlbumPerate,{
	
	albumId:"",
	getListUrl:"",
	transferUrl:"",
	chooseAlbum:"",
	constructor:function(obj){
	console.log("albumId-------------->"+this.albumId);
		dojo.query("#"+this.transferId).onclick(dojo.hitch(this, "transferPic"));
		dojo.connect(dijit.byId(this.chooseAlbum),"execute",dojo.hitch(this,"postChooseAlbum"));
	},
	postChooseAlbum:function(){
		var albumIdB = dojo.byId('combo').value;
		var urladdr = this.transferUrl+"&ids="+getIds()+"&albumIdB="+albumIdB;
		this.postService(urladdr);		
	},
	transferPic:function(){
		var picIds = getIds();
		if(picIds){
			this.getAlbumJsonList();	
			dojo.query('#chooseAlbum').style("display:block");
			dijit.byId('chooseAlbum').show();
		}
	},
	getAlbumJsonList:function(){
		console.log("3-------------->"+this.albumId);
		dojo.xhrGet({
			url:this.getListUrl, 
			load:dojo.hitch(this,"insertOption"),
			handleAs: "json"
		});
	},
	insertOption:function(arguments){
		console.log("1-------------->"+this.albumId);
		var items=arguments;
		var combo = dojo.byId("combo");
		var index = 0;
		for(var i=0; i<items.length; i++){
			if(this.albumId!=items[i].id){					
				var oOption=new Option(items[i].name,items[i].id);
		  		combo.options[index]=oOption;
		  		index++;
			} 
		}
	}
});
		
dojo.declare("dojot.album.albumPicDelete",dojot.album.AlbumPerate,{
	albumId:"",
	deletePicURL:"",
	constructor: function(obj){
		this.albumId = obj.albumId;
		this.deletePicURL = obj.deletePicURL;
		dojo.query("#deletePic").onclick(dojo.hitch(this, "confirmDelete"))
	},
	confirmDelete: function(){
		var picIds = getIds();
		if(picIds){	
			if(confirm(this._messages.confirmDelete)){
				this.deletePic(picIds);
			}								
		}			
	},					
	deletePic:function(picIds){		
		var urlads =this.deletePicURL+picIds;
		this.postService(urlads,function(){
			dojo.query("input:checked").forEach(function(data){
				data.checked=false;
			});			
		});	
	}
});

dojo.declare("dojot.album.albumDelete",null,{
	albumId:"",
	attr:"",
	deleteUrl:'',
	returnUrl:'',
	constructor:function(obj){
		dojo.mixin(this,obj);
		this._message1 = dojo.i18n.getLocalization("dojot", "album",dojo.locale);
		dojo.query(this.attr).onclick(dojo.hitch(this, 'deleteAlbum'));
	},						
	deleteAlbum:function(){
		var that = this;
		if(confirm(this._messages.album2AlbumDelete)){
			dojo.xhrPost({
				url:that.deleteUrl+that.albumId,
				load:function(responseDate){
		 			if(responseDate){
						window.location.href=that.returnUrl;				
					}else{
						alert(that._messages.deleteFailture);
					}
				}  		 		
			});
		 }
	}
});
